package csc.io;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Simple writer fot storing data.
 * @author Stanislau Dubrouski
 */
public class FileWriter {

    /**
     * Writes content to file fileName. If file doesn not exist, it will be created
     * @param fileName
     * @param content
     * @return true if no errors occured, false otherwise
     */
    public static boolean writeToFile(String fileName, String content) {
        File file = new File(fileName);
        FileOutputStream fop;
        try {
            fop = new FileOutputStream(file);            
            // if file doesn't exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }
            // get the content in bytes
            byte[] contentInBytes = content.getBytes();

            fop.write(contentInBytes);
            fop.flush();
            fop.close();

        } catch (IOException e) {
            return false;
        }
        return true;
    }
}
